﻿-- Function: public.timeserial_treedata(integer, integer, integer)

--- DROP FUNCTION public.timeserial_treedata(integer, integer, integer);

CREATE OR REPLACE FUNCTION public.timeserial_tree(
    v_calendar_enum varchar,
    p_org_id integer)
  RETURNS json  AS
$BODY$
declare  
    v_timeserialstatus_enum varchar(100);
    v_timestructid  integer;
    v_type integer;
    v_datatree	json;
begin
   select COALESCE(min(id),0)
   into v_timestructid
   From timestruct where org_id=p_org_id and calendar_enum = v_calendar_enum  and parent_id=0;
   if v_timestructid=0 then---如果时间序列结构ID为0，只显示已打开，否则显示打开和已关闭
     v_timeserialstatus_enum ='active';
     v_type =0;
   else
     v_timeserialstatus_enum ='active,freeze,publish';
     v_type=1;
   end if;
   v_datatree = timeserial_treedata(v_type,v_timestructid,0,v_timeserialstatus_enum,p_org_id);
   RETURN v_datatree; 
END;  
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
